package com.linkstec.mot.serializer;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linkstec.mot.util.ExceptionTracker;
import com.linkstec.mot.util.StringUtil;

/**
 * 默认String类型解析器
 * 
 * @author PENGYAO
 * 
 */
public class StringParser implements IParser {

	private static Logger logger = LoggerFactory.getLogger(StringParser.class);

	@Override
	public Object parse(byte[] data) {
		try {
			return fromBytes(data);
		} catch (UnsupportedEncodingException e) {
			logger.error(ExceptionTracker.trace(e));
		}
		return null;
	}

	public String encoding() {
		return encoding;
	}

	public String fromBytes(byte bytes[]) throws UnsupportedEncodingException {
		return new String(bytes, encoding());
	}

	public StringParser(String bianma) {
		if (StringUtil.isNullOrEmpty(bianma)) {
			encoding = "UTF8";
		} else {
			encoding = bianma;
		}
	}
	
	private final String encoding;

}
